﻿package game 
{	
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.net.URLRequest;
	
	import game.events.EventData;

	/**
	 * ...
	 * @author Doan Nguyen Thuy Nha Truc (Bamboo)
	 */
	public class LoadMc extends EventDispatcher
	{
		public var myUrlRequest:URLRequest;
		public var myLoader:Loader;
		public var movieChild:Object;
		
		public function LoadMc() 
		{
			
		}
		
		public function loadSwf(nameSwf:String,gameDepthKeeper:MovieClip) {
			if (movieChild != null) {
				unloadSwf();
			}
			trace("vo load swf");
			myUrlRequest = new URLRequest(nameSwf);
			myLoader = new Loader();
			myLoader.load(myUrlRequest);
			
			gameDepthKeeper.addChild(myLoader);
			configureListeners(myLoader.contentLoaderInfo);
		}
		
		private function hdlCompleteSwf(e:Event):void 
		{
			movieChild = e.target.content;
			trace("movieChild : " + movieChild);
		}
		
		private function configureListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.COMPLETE, completeHandler);
            dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            dispatcher.addEventListener(Event.INIT, initHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            dispatcher.addEventListener(Event.OPEN, openHandler);
            dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            dispatcher.addEventListener(Event.UNLOAD, unLoadHandler);
        }

        private function completeHandler(event:Event):void {
            //trace("completeHandler: " + event);
			movieChild = event.target.content;
			var e:EventData = new EventData(EventData.LOAD_MOVIE);
			e.ms = "completeHandler";
			dispatchEvent(e);
        }

        private function httpStatusHandler(event:HTTPStatusEvent):void {
            //trace("httpStatusHandler: " + event);
			var e:EventData = new EventData(EventData.LOAD_MOVIE);
			e.ms = "httpStatusHandler";
			dispatchEvent(e);
        }

        private function initHandler(event:Event):void {
            //trace("initHandler: " + event);
        }

        private function ioErrorHandler(event:IOErrorEvent):void {
		
            //trace("ioErrorHandler: " + event);
			var e:EventData = new EventData(EventData.LOAD_MOVIE);
			e.ms = "ioErrorHandler";
			dispatchEvent(e);
        }

        private function openHandler(event:Event):void {
            //trace("openHandler: " + event);
        }

        private function progressHandler(event:ProgressEvent):void {
            //trace("progressHandler: bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
        }

        private function unLoadHandler(event:Event):void {
            trace("unLoadHandler: " + event);
        }
		
		public function unloadSwf():void {
			trace("unloadSwf");
			//myLoader.unloadAndStop();
			movieChild=null;
		}

	}

}